function GeojsonData(viewer) {
    this._viewer = viewer;
    this._dataSource = null;
    GeojsonData.GEOJSON = "geojson";
    GeojsonData.JSON = "json";
    this._pointPos = null;
}

GeojsonData.getInstance = function() {
    if (GeojsonData.__ginstance == null) {
        GeojsonData.__ginstance = new GeojsonData(window.cesiumViewer);
    }
    return GeojsonData.__ginstance;
};
GeojsonData.prototype.initData = function(that, name) {
    let positionResult = document.getElementById("positionResult");
    if (name === "") {
        that.$message({
            showClose: true,
            message: "数据不能为空",
            type: "warning"
        });
        return;
    }
    /*--------------------------------*/
    if (name.indexOf("，") !== -1) {
        that.$message({
            showClose: true,
            message: "请选择英文逗号",
            type: "error"
        });
        if (positionResult.style.display === "block") {
            positionResult.style.display = "none";
        }
        return;
    }
    if (name.indexOf(",") !== -1) {
        const floatlatregex = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)$/;
        /* const floatlonregex = /^[-+]?((1[0-7]\d{1}|0?\d{1,2})\.\d{1,5}|180\.0*)$/;*/
        const floatlonregex = /^-?((0|1?[0-8]?[0-9]?)(([.][0-9]{1,10})?)|180(([.][0]{1,10})?))$/;
        let lon = parseFloat(name.split(",")[0]);

        let lat = parseFloat(name.split(",")[1]);
        if (!floatlonregex.test(name.split(",")[0])) {
            /*    alert("经度填写错误");*/
            that.$message({
                showClose: true,
                message: "经度填写错误",
                type: "error"
            });
            if (positionResult.style.display === "block") {
                positionResult.style.display = "none";
            }
            return;
        }
        if (!floatlatregex.test(name.split(",")[1])) {
            that.$message({
                showClose: true,
                message: "纬度填写错误",
                type: "error"
            });
            if (positionResult.style.display === "block") {
                positionResult.style.display = "none";
            }
            return;
        }
        GeojsonData.getInstance().createPosotionPoint(name, lon, lat);
    } else {
        let s = JSON.parse(JavaContext.getFileForlderNamesByPosName(name));
        if (s.length === 0) {
            that.$message({
                showClose: true,
                message: "未查到数据",
                type: "warning"
            });
            if (positionResult.style.display === "block") {
                positionResult.style.display = "none";
            }
            return;
        }

        if (positionResult.style.display === "none") {
            positionResult.style.display = "block";
        }
        that.tableData = undefined;
        that.tableData = new Array();

        s.forEach(function(item) {
            let obj = {
                posName: item.posName,
                uuid: item.uuid,
                name: item.name
            };

            that.tableData.push(obj);
        });
        that.tableCount = that.tableData.length;
        that.posDatas = s;
    }
};
GeojsonData.prototype.createPosotionPoint = function(name, lon, lat) {
    let that = this;
    if (that._pointPos !== null) {
        that._viewer.entities.remove(that._pointPos);
    }
    let point = that._viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(lon, lat),
        // label: {
        //     //文字标签
        //     text: name,
        //     font: "24px Helvetica",
        //     fillColor: Cesium.Color.SKYBLUE,
        //     outlineColor: Cesium.Color.BLACK,
        //     outlineWidth: 2,
        //     disableDepthTestDistance: Number.POSITIVE_INFINITY,
        //     style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        //     horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
        //     verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        //     //中心位置
        //     pixelOffset: new Cesium.Cartesian2(20, -10)
        // },
        // point: {
        //     pixelSize: 12,
        //     color: Cesium.Color.RED,
        //     outlineColor: Cesium.Color.WHITE,
        //     outlineWidth: 2,
        //     horizontalOrigin: Cesium.HorizontalOrigin.RIGHT,
        //     verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        //     heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        // }
        billboard: {
            image: "./images/red.png",
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.CENTER,
            scale: 0.7,
            // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(that._data.minshow, that._data.maxshow),
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        }
    });
    that._pointPos = point;
    that._viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(lon, lat, 10000),
        orientation: {
            heading: Cesium.Math.toRadians(0.0),
            pitch: Cesium.Math.toRadians(-90.0),
            roll: 0.0
        }
    });

    //关闭地名列表
    let positionResult = document.getElementById("positionResult");
    if (positionResult.style.display === "block") {
        positionResult.style.display = "none";
        that.tableData = undefined;
        that.tableData = new Array();
    }
};
GeojsonData.prototype.clear = function() {
    let that = this;
    if (that._dataSource !== null) {
        that._viewer.dataSources.remove(that._dataSource, true);
    }
    if (that._point) {
        that._viewer.entities.remove(that._point);
    }

    if (that._pointPos !== null) {
        that._viewer.entities.remove(that._pointPos);
    }
};

export default GeojsonData;
